/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.slayer.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.RowMapper;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ImagePersistence;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.slayer.NoSuchBookException;

import com.slayer.model.BMSBook;
import com.slayer.model.impl.BMSBookImpl;
import com.slayer.model.impl.BMSBookModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the b m s book service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Hemant
 * @see BMSBookPersistence
 * @see BMSBookUtil
 * @generated
 */
public class BMSBookPersistenceImpl extends BasePersistenceImpl<BMSBook>
	implements BMSBookPersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link BMSBookUtil} to access the b m s book persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = BMSBookImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_BOOKTITLE =
		new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, BMSBookImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByBookTitle",
			new String[] {
				String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BOOKTITLE =
		new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, BMSBookImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByBookTitle",
			new String[] { String.class.getName() },
			BMSBookModelImpl.BOOKTITLE_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_BOOKTITLE = new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByBookTitle",
			new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR = new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, BMSBookImpl.class,
			FINDER_CLASS_NAME_ENTITY, "fetchByBookTitle_Author",
			new String[] { String.class.getName(), String.class.getName() },
			BMSBookModelImpl.BOOKTITLE_COLUMN_BITMASK |
			BMSBookModelImpl.AUTHOR_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_BOOKTITLE_AUTHOR = new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"countByBookTitle_Author",
			new String[] { String.class.getName(), String.class.getName() });
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, BMSBookImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, BMSBookImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);

	/**
	 * Caches the b m s book in the entity cache if it is enabled.
	 *
	 * @param bmsBook the b m s book
	 */
	public void cacheResult(BMSBook bmsBook) {
		EntityCacheUtil.putResult(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookImpl.class, bmsBook.getPrimaryKey(), bmsBook);

		FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
			new Object[] { bmsBook.getBookTitle(), bmsBook.getAuthor() },
			bmsBook);

		bmsBook.resetOriginalValues();
	}

	/**
	 * Caches the b m s books in the entity cache if it is enabled.
	 *
	 * @param bmsBooks the b m s books
	 */
	public void cacheResult(List<BMSBook> bmsBooks) {
		for (BMSBook bmsBook : bmsBooks) {
			if (EntityCacheUtil.getResult(
						BMSBookModelImpl.ENTITY_CACHE_ENABLED,
						BMSBookImpl.class, bmsBook.getPrimaryKey()) == null) {
				cacheResult(bmsBook);
			}
			else {
				bmsBook.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all b m s books.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(BMSBookImpl.class.getName());
		}

		EntityCacheUtil.clearCache(BMSBookImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the b m s book.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(BMSBook bmsBook) {
		EntityCacheUtil.removeResult(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookImpl.class, bmsBook.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		clearUniqueFindersCache(bmsBook);
	}

	@Override
	public void clearCache(List<BMSBook> bmsBooks) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (BMSBook bmsBook : bmsBooks) {
			EntityCacheUtil.removeResult(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
				BMSBookImpl.class, bmsBook.getPrimaryKey());

			clearUniqueFindersCache(bmsBook);
		}
	}

	protected void clearUniqueFindersCache(BMSBook bmsBook) {
		FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
			new Object[] { bmsBook.getBookTitle(), bmsBook.getAuthor() });
	}

	/**
	 * Creates a new b m s book with the primary key. Does not add the b m s book to the database.
	 *
	 * @param bookId the primary key for the new b m s book
	 * @return the new b m s book
	 */
	public BMSBook create(long bookId) {
		BMSBook bmsBook = new BMSBookImpl();

		bmsBook.setNew(true);
		bmsBook.setPrimaryKey(bookId);

		return bmsBook;
	}

	/**
	 * Removes the b m s book with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param bookId the primary key of the b m s book
	 * @return the b m s book that was removed
	 * @throws com.slayer.NoSuchBookException if a b m s book with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook remove(long bookId)
		throws NoSuchBookException, SystemException {
		return remove(Long.valueOf(bookId));
	}

	/**
	 * Removes the b m s book with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the b m s book
	 * @return the b m s book that was removed
	 * @throws com.slayer.NoSuchBookException if a b m s book with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public BMSBook remove(Serializable primaryKey)
		throws NoSuchBookException, SystemException {
		Session session = null;

		try {
			session = openSession();

			BMSBook bmsBook = (BMSBook)session.get(BMSBookImpl.class, primaryKey);

			if (bmsBook == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchBookException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(bmsBook);
		}
		catch (NoSuchBookException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected BMSBook removeImpl(BMSBook bmsBook) throws SystemException {
		bmsBook = toUnwrappedModel(bmsBook);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, bmsBook);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		clearCache(bmsBook);

		return bmsBook;
	}

	@Override
	public BMSBook updateImpl(com.slayer.model.BMSBook bmsBook, boolean merge)
		throws SystemException {
		bmsBook = toUnwrappedModel(bmsBook);

		boolean isNew = bmsBook.isNew();

		BMSBookModelImpl bmsBookModelImpl = (BMSBookModelImpl)bmsBook;

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, bmsBook, merge);

			bmsBook.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew || !BMSBookModelImpl.COLUMN_BITMASK_ENABLED) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		else {
			if ((bmsBookModelImpl.getColumnBitmask() &
					FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BOOKTITLE.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						bmsBookModelImpl.getOriginalBookTitle()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_BOOKTITLE,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BOOKTITLE,
					args);

				args = new Object[] { bmsBookModelImpl.getBookTitle() };

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_BOOKTITLE,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BOOKTITLE,
					args);
			}
		}

		EntityCacheUtil.putResult(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
			BMSBookImpl.class, bmsBook.getPrimaryKey(), bmsBook);

		if (isNew) {
			FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
				new Object[] { bmsBook.getBookTitle(), bmsBook.getAuthor() },
				bmsBook);
		}
		else {
			if ((bmsBookModelImpl.getColumnBitmask() &
					FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						bmsBookModelImpl.getOriginalBookTitle(),
						
						bmsBookModelImpl.getOriginalAuthor()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_BOOKTITLE_AUTHOR,
					args);

				FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
					args);

				FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
					new Object[] { bmsBook.getBookTitle(), bmsBook.getAuthor() },
					bmsBook);
			}
		}

		return bmsBook;
	}

	protected BMSBook toUnwrappedModel(BMSBook bmsBook) {
		if (bmsBook instanceof BMSBookImpl) {
			return bmsBook;
		}

		BMSBookImpl bmsBookImpl = new BMSBookImpl();

		bmsBookImpl.setNew(bmsBook.isNew());
		bmsBookImpl.setPrimaryKey(bmsBook.getPrimaryKey());

		bmsBookImpl.setBookId(bmsBook.getBookId());
		bmsBookImpl.setBookTitle(bmsBook.getBookTitle());
		bmsBookImpl.setAuthor(bmsBook.getAuthor());
		bmsBookImpl.setCreateDate(bmsBook.getCreateDate());
		bmsBookImpl.setModifiedDate(bmsBook.getModifiedDate());

		return bmsBookImpl;
	}

	/**
	 * Returns the b m s book with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the b m s book
	 * @return the b m s book
	 * @throws com.liferay.portal.NoSuchModelException if a b m s book with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public BMSBook findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the b m s book with the primary key or throws a {@link com.slayer.NoSuchBookException} if it could not be found.
	 *
	 * @param bookId the primary key of the b m s book
	 * @return the b m s book
	 * @throws com.slayer.NoSuchBookException if a b m s book with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook findByPrimaryKey(long bookId)
		throws NoSuchBookException, SystemException {
		BMSBook bmsBook = fetchByPrimaryKey(bookId);

		if (bmsBook == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + bookId);
			}

			throw new NoSuchBookException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				bookId);
		}

		return bmsBook;
	}

	/**
	 * Returns the b m s book with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the b m s book
	 * @return the b m s book, or <code>null</code> if a b m s book with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public BMSBook fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the b m s book with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param bookId the primary key of the b m s book
	 * @return the b m s book, or <code>null</code> if a b m s book with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook fetchByPrimaryKey(long bookId) throws SystemException {
		BMSBook bmsBook = (BMSBook)EntityCacheUtil.getResult(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
				BMSBookImpl.class, bookId);

		if (bmsBook == _nullBMSBook) {
			return null;
		}

		if (bmsBook == null) {
			Session session = null;

			boolean hasException = false;

			try {
				session = openSession();

				bmsBook = (BMSBook)session.get(BMSBookImpl.class,
						Long.valueOf(bookId));
			}
			catch (Exception e) {
				hasException = true;

				throw processException(e);
			}
			finally {
				if (bmsBook != null) {
					cacheResult(bmsBook);
				}
				else if (!hasException) {
					EntityCacheUtil.putResult(BMSBookModelImpl.ENTITY_CACHE_ENABLED,
						BMSBookImpl.class, bookId, _nullBMSBook);
				}

				closeSession(session);
			}
		}

		return bmsBook;
	}

	/**
	 * Returns all the b m s books where bookTitle = &#63;.
	 *
	 * @param bookTitle the book title
	 * @return the matching b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public List<BMSBook> findByBookTitle(String bookTitle)
		throws SystemException {
		return findByBookTitle(bookTitle, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
			null);
	}

	/**
	 * Returns a range of all the b m s books where bookTitle = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param bookTitle the book title
	 * @param start the lower bound of the range of b m s books
	 * @param end the upper bound of the range of b m s books (not inclusive)
	 * @return the range of matching b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public List<BMSBook> findByBookTitle(String bookTitle, int start, int end)
		throws SystemException {
		return findByBookTitle(bookTitle, start, end, null);
	}

	/**
	 * Returns an ordered range of all the b m s books where bookTitle = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param bookTitle the book title
	 * @param start the lower bound of the range of b m s books
	 * @param end the upper bound of the range of b m s books (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of matching b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public List<BMSBook> findByBookTitle(String bookTitle, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BOOKTITLE;
			finderArgs = new Object[] { bookTitle };
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_BOOKTITLE;
			finderArgs = new Object[] { bookTitle, start, end, orderByComparator };
		}

		List<BMSBook> list = (List<BMSBook>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if ((list != null) && !list.isEmpty()) {
			for (BMSBook bmsBook : list) {
				if (!Validator.equals(bookTitle, bmsBook.getBookTitle())) {
					list = null;

					break;
				}
			}
		}

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_BMSBOOK_WHERE);

			if (bookTitle == null) {
				query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_1);
			}
			else {
				if (bookTitle.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_3);
				}
				else {
					query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_2);
				}
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(BMSBookModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (bookTitle != null) {
					qPos.add(bookTitle);
				}

				list = (List<BMSBook>)QueryUtil.list(q, getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Returns the first b m s book in the ordered set where bookTitle = &#63;.
	 *
	 * @param bookTitle the book title
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching b m s book
	 * @throws com.slayer.NoSuchBookException if a matching b m s book could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook findByBookTitle_First(String bookTitle,
		OrderByComparator orderByComparator)
		throws NoSuchBookException, SystemException {
		BMSBook bmsBook = fetchByBookTitle_First(bookTitle, orderByComparator);

		if (bmsBook != null) {
			return bmsBook;
		}

		StringBundler msg = new StringBundler(4);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("bookTitle=");
		msg.append(bookTitle);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchBookException(msg.toString());
	}

	/**
	 * Returns the first b m s book in the ordered set where bookTitle = &#63;.
	 *
	 * @param bookTitle the book title
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching b m s book, or <code>null</code> if a matching b m s book could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook fetchByBookTitle_First(String bookTitle,
		OrderByComparator orderByComparator) throws SystemException {
		List<BMSBook> list = findByBookTitle(bookTitle, 0, 1, orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the last b m s book in the ordered set where bookTitle = &#63;.
	 *
	 * @param bookTitle the book title
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching b m s book
	 * @throws com.slayer.NoSuchBookException if a matching b m s book could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook findByBookTitle_Last(String bookTitle,
		OrderByComparator orderByComparator)
		throws NoSuchBookException, SystemException {
		BMSBook bmsBook = fetchByBookTitle_Last(bookTitle, orderByComparator);

		if (bmsBook != null) {
			return bmsBook;
		}

		StringBundler msg = new StringBundler(4);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("bookTitle=");
		msg.append(bookTitle);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchBookException(msg.toString());
	}

	/**
	 * Returns the last b m s book in the ordered set where bookTitle = &#63;.
	 *
	 * @param bookTitle the book title
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching b m s book, or <code>null</code> if a matching b m s book could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook fetchByBookTitle_Last(String bookTitle,
		OrderByComparator orderByComparator) throws SystemException {
		int count = countByBookTitle(bookTitle);

		List<BMSBook> list = findByBookTitle(bookTitle, count - 1, count,
				orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the b m s books before and after the current b m s book in the ordered set where bookTitle = &#63;.
	 *
	 * @param bookId the primary key of the current b m s book
	 * @param bookTitle the book title
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the previous, current, and next b m s book
	 * @throws com.slayer.NoSuchBookException if a b m s book with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook[] findByBookTitle_PrevAndNext(long bookId, String bookTitle,
		OrderByComparator orderByComparator)
		throws NoSuchBookException, SystemException {
		BMSBook bmsBook = findByPrimaryKey(bookId);

		Session session = null;

		try {
			session = openSession();

			BMSBook[] array = new BMSBookImpl[3];

			array[0] = getByBookTitle_PrevAndNext(session, bmsBook, bookTitle,
					orderByComparator, true);

			array[1] = bmsBook;

			array[2] = getByBookTitle_PrevAndNext(session, bmsBook, bookTitle,
					orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected BMSBook getByBookTitle_PrevAndNext(Session session,
		BMSBook bmsBook, String bookTitle, OrderByComparator orderByComparator,
		boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_BMSBOOK_WHERE);

		if (bookTitle == null) {
			query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_1);
		}
		else {
			if (bookTitle.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_3);
			}
			else {
				query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

			if (orderByConditionFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByConditionFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByConditionFields[i]);

				if ((i + 1) < orderByConditionFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			String[] orderByFields = orderByComparator.getOrderByFields();

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(BMSBookModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		if (bookTitle != null) {
			qPos.add(bookTitle);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByConditionValues(bmsBook);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<BMSBook> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Returns the b m s book where bookTitle = &#63; and author = &#63; or throws a {@link com.slayer.NoSuchBookException} if it could not be found.
	 *
	 * @param bookTitle the book title
	 * @param author the author
	 * @return the matching b m s book
	 * @throws com.slayer.NoSuchBookException if a matching b m s book could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook findByBookTitle_Author(String bookTitle, String author)
		throws NoSuchBookException, SystemException {
		BMSBook bmsBook = fetchByBookTitle_Author(bookTitle, author);

		if (bmsBook == null) {
			StringBundler msg = new StringBundler(6);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("bookTitle=");
			msg.append(bookTitle);

			msg.append(", author=");
			msg.append(author);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			if (_log.isWarnEnabled()) {
				_log.warn(msg.toString());
			}

			throw new NoSuchBookException(msg.toString());
		}

		return bmsBook;
	}

	/**
	 * Returns the b m s book where bookTitle = &#63; and author = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
	 *
	 * @param bookTitle the book title
	 * @param author the author
	 * @return the matching b m s book, or <code>null</code> if a matching b m s book could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook fetchByBookTitle_Author(String bookTitle, String author)
		throws SystemException {
		return fetchByBookTitle_Author(bookTitle, author, true);
	}

	/**
	 * Returns the b m s book where bookTitle = &#63; and author = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
	 *
	 * @param bookTitle the book title
	 * @param author the author
	 * @param retrieveFromCache whether to use the finder cache
	 * @return the matching b m s book, or <code>null</code> if a matching b m s book could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook fetchByBookTitle_Author(String bookTitle, String author,
		boolean retrieveFromCache) throws SystemException {
		Object[] finderArgs = new Object[] { bookTitle, author };

		Object result = null;

		if (retrieveFromCache) {
			result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
					finderArgs, this);
		}

		if (result instanceof BMSBook) {
			BMSBook bmsBook = (BMSBook)result;

			if (!Validator.equals(bookTitle, bmsBook.getBookTitle()) ||
					!Validator.equals(author, bmsBook.getAuthor())) {
				result = null;
			}
		}

		if (result == null) {
			StringBundler query = new StringBundler(4);

			query.append(_SQL_SELECT_BMSBOOK_WHERE);

			if (bookTitle == null) {
				query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_1);
			}
			else {
				if (bookTitle.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_3);
				}
				else {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_2);
				}
			}

			if (author == null) {
				query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_1);
			}
			else {
				if (author.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_3);
				}
				else {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_2);
				}
			}

			query.append(BMSBookModelImpl.ORDER_BY_JPQL);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (bookTitle != null) {
					qPos.add(bookTitle);
				}

				if (author != null) {
					qPos.add(author);
				}

				List<BMSBook> list = q.list();

				result = list;

				BMSBook bmsBook = null;

				if (list.isEmpty()) {
					FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
						finderArgs, list);
				}
				else {
					bmsBook = list.get(0);

					cacheResult(bmsBook);

					if ((bmsBook.getBookTitle() == null) ||
							!bmsBook.getBookTitle().equals(bookTitle) ||
							(bmsBook.getAuthor() == null) ||
							!bmsBook.getAuthor().equals(author)) {
						FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
							finderArgs, bmsBook);
					}
				}

				return bmsBook;
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (result == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_BOOKTITLE_AUTHOR,
						finderArgs);
				}

				closeSession(session);
			}
		}
		else {
			if (result instanceof List<?>) {
				return null;
			}
			else {
				return (BMSBook)result;
			}
		}
	}

	/**
	 * Returns all the b m s books.
	 *
	 * @return the b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public List<BMSBook> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the b m s books.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of b m s books
	 * @param end the upper bound of the range of b m s books (not inclusive)
	 * @return the range of b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public List<BMSBook> findAll(int start, int end) throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the b m s books.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of b m s books
	 * @param end the upper bound of the range of b m s books (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public List<BMSBook> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = new Object[] { start, end, orderByComparator };

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<BMSBook> list = (List<BMSBook>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_BMSBOOK);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_BMSBOOK.concat(BMSBookModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<BMSBook>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<BMSBook>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the b m s books where bookTitle = &#63; from the database.
	 *
	 * @param bookTitle the book title
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByBookTitle(String bookTitle) throws SystemException {
		for (BMSBook bmsBook : findByBookTitle(bookTitle)) {
			remove(bmsBook);
		}
	}

	/**
	 * Removes the b m s book where bookTitle = &#63; and author = &#63; from the database.
	 *
	 * @param bookTitle the book title
	 * @param author the author
	 * @return the b m s book that was removed
	 * @throws SystemException if a system exception occurred
	 */
	public BMSBook removeByBookTitle_Author(String bookTitle, String author)
		throws NoSuchBookException, SystemException {
		BMSBook bmsBook = findByBookTitle_Author(bookTitle, author);

		return remove(bmsBook);
	}

	/**
	 * Removes all the b m s books from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (BMSBook bmsBook : findAll()) {
			remove(bmsBook);
		}
	}

	/**
	 * Returns the number of b m s books where bookTitle = &#63;.
	 *
	 * @param bookTitle the book title
	 * @return the number of matching b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public int countByBookTitle(String bookTitle) throws SystemException {
		Object[] finderArgs = new Object[] { bookTitle };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_BOOKTITLE,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_BMSBOOK_WHERE);

			if (bookTitle == null) {
				query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_1);
			}
			else {
				if (bookTitle.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_3);
				}
				else {
					query.append(_FINDER_COLUMN_BOOKTITLE_BOOKTITLE_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (bookTitle != null) {
					qPos.add(bookTitle);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_BOOKTITLE,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of b m s books where bookTitle = &#63; and author = &#63;.
	 *
	 * @param bookTitle the book title
	 * @param author the author
	 * @return the number of matching b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public int countByBookTitle_Author(String bookTitle, String author)
		throws SystemException {
		Object[] finderArgs = new Object[] { bookTitle, author };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_BOOKTITLE_AUTHOR,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(3);

			query.append(_SQL_COUNT_BMSBOOK_WHERE);

			if (bookTitle == null) {
				query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_1);
			}
			else {
				if (bookTitle.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_3);
				}
				else {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_2);
				}
			}

			if (author == null) {
				query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_1);
			}
			else {
				if (author.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_3);
				}
				else {
					query.append(_FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (bookTitle != null) {
					qPos.add(bookTitle);
				}

				if (author != null) {
					qPos.add(author);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_BOOKTITLE_AUTHOR,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of b m s books.
	 *
	 * @return the number of b m s books
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_BMSBOOK);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns all the b m s borrowings associated with the b m s book.
	 *
	 * @param pk the primary key of the b m s book
	 * @return the b m s borrowings associated with the b m s book
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.slayer.model.BMSBorrowing> getBMSBorrowings(long pk)
		throws SystemException {
		return getBMSBorrowings(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
	}

	/**
	 * Returns a range of all the b m s borrowings associated with the b m s book.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the b m s book
	 * @param start the lower bound of the range of b m s books
	 * @param end the upper bound of the range of b m s books (not inclusive)
	 * @return the range of b m s borrowings associated with the b m s book
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.slayer.model.BMSBorrowing> getBMSBorrowings(long pk,
		int start, int end) throws SystemException {
		return getBMSBorrowings(pk, start, end, null);
	}

	public static final FinderPath FINDER_PATH_GET_BMSBORROWINGS = new FinderPath(com.slayer.model.impl.BMSBorrowingModelImpl.ENTITY_CACHE_ENABLED,
			com.slayer.model.impl.BMSBorrowingModelImpl.FINDER_CACHE_ENABLED,
			com.slayer.model.impl.BMSBorrowingImpl.class,
			com.slayer.service.persistence.BMSBorrowingPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getBMSBorrowings",
			new String[] {
				Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});

	static {
		FINDER_PATH_GET_BMSBORROWINGS.setCacheKeyGeneratorCacheName(null);
	}

	/**
	 * Returns an ordered range of all the b m s borrowings associated with the b m s book.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the b m s book
	 * @param start the lower bound of the range of b m s books
	 * @param end the upper bound of the range of b m s books (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of b m s borrowings associated with the b m s book
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.slayer.model.BMSBorrowing> getBMSBorrowings(long pk,
		int start, int end, OrderByComparator orderByComparator)
		throws SystemException {
		Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };

		List<com.slayer.model.BMSBorrowing> list = (List<com.slayer.model.BMSBorrowing>)FinderCacheUtil.getResult(FINDER_PATH_GET_BMSBORROWINGS,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				String sql = null;

				if (orderByComparator != null) {
					sql = _SQL_GETBMSBORROWINGS.concat(ORDER_BY_CLAUSE)
											   .concat(orderByComparator.getOrderBy());
				}
				else {
					sql = _SQL_GETBMSBORROWINGS;
				}

				SQLQuery q = session.createSQLQuery(sql);

				q.addEntity("BMS_BMSBorrowing",
					com.slayer.model.impl.BMSBorrowingImpl.class);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				list = (List<com.slayer.model.BMSBorrowing>)QueryUtil.list(q,
						getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_GET_BMSBORROWINGS,
						finderArgs);
				}
				else {
					bmsBorrowingPersistence.cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_GET_BMSBORROWINGS,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	public static final FinderPath FINDER_PATH_GET_BMSBORROWINGS_SIZE = new FinderPath(com.slayer.model.impl.BMSBorrowingModelImpl.ENTITY_CACHE_ENABLED,
			com.slayer.model.impl.BMSBorrowingModelImpl.FINDER_CACHE_ENABLED,
			com.slayer.model.impl.BMSBorrowingImpl.class,
			com.slayer.service.persistence.BMSBorrowingPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getBMSBorrowingsSize", new String[] { Long.class.getName() });

	static {
		FINDER_PATH_GET_BMSBORROWINGS_SIZE.setCacheKeyGeneratorCacheName(null);
	}

	/**
	 * Returns the number of b m s borrowings associated with the b m s book.
	 *
	 * @param pk the primary key of the b m s book
	 * @return the number of b m s borrowings associated with the b m s book
	 * @throws SystemException if a system exception occurred
	 */
	public int getBMSBorrowingsSize(long pk) throws SystemException {
		Object[] finderArgs = new Object[] { pk };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_BMSBORROWINGS_SIZE,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				SQLQuery q = session.createSQLQuery(_SQL_GETBMSBORROWINGSSIZE);

				q.addScalar(COUNT_COLUMN_NAME,
					com.liferay.portal.kernel.dao.orm.Type.LONG);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_GET_BMSBORROWINGS_SIZE,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	public static final FinderPath FINDER_PATH_CONTAINS_BMSBORROWING = new FinderPath(com.slayer.model.impl.BMSBorrowingModelImpl.ENTITY_CACHE_ENABLED,
			com.slayer.model.impl.BMSBorrowingModelImpl.FINDER_CACHE_ENABLED,
			com.slayer.model.impl.BMSBorrowingImpl.class,
			com.slayer.service.persistence.BMSBorrowingPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"containsBMSBorrowing",
			new String[] { Long.class.getName(), Long.class.getName() });

	/**
	 * Returns <code>true</code> if the b m s borrowing is associated with the b m s book.
	 *
	 * @param pk the primary key of the b m s book
	 * @param bmsBorrowingPK the primary key of the b m s borrowing
	 * @return <code>true</code> if the b m s borrowing is associated with the b m s book; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsBMSBorrowing(long pk, long bmsBorrowingPK)
		throws SystemException {
		Object[] finderArgs = new Object[] { pk, bmsBorrowingPK };

		Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_BMSBORROWING,
				finderArgs, this);

		if (value == null) {
			try {
				value = Boolean.valueOf(containsBMSBorrowing.contains(pk,
							bmsBorrowingPK));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (value == null) {
					value = Boolean.FALSE;
				}

				FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_BMSBORROWING,
					finderArgs, value);
			}
		}

		return value.booleanValue();
	}

	/**
	 * Returns <code>true</code> if the b m s book has any b m s borrowings associated with it.
	 *
	 * @param pk the primary key of the b m s book to check for associations with b m s borrowings
	 * @return <code>true</code> if the b m s book has any b m s borrowings associated with it; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsBMSBorrowings(long pk) throws SystemException {
		if (getBMSBorrowingsSize(pk) > 0) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * Initializes the b m s book persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.slayer.model.BMSBook")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<BMSBook>> listenersList = new ArrayList<ModelListener<BMSBook>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<BMSBook>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}

		containsBMSBorrowing = new ContainsBMSBorrowing();
	}

	public void destroy() {
		EntityCacheUtil.removeCache(BMSBookImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@BeanReference(type = BMSBookPersistence.class)
	protected BMSBookPersistence bmsBookPersistence;
	@BeanReference(type = BMSBorrowingPersistence.class)
	protected BMSBorrowingPersistence bmsBorrowingPersistence;
	@BeanReference(type = ImagePersistence.class)
	protected ImagePersistence imagePersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	protected ContainsBMSBorrowing containsBMSBorrowing;

	protected class ContainsBMSBorrowing {
		protected ContainsBMSBorrowing() {
			_mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
					_SQL_CONTAINSBMSBORROWING,
					new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
					RowMapper.COUNT);
		}

		protected boolean contains(long bookId, long borrowingId) {
			List<Integer> results = _mappingSqlQuery.execute(new Object[] {
						new Long(bookId), new Long(borrowingId)
					});

			if (results.size() > 0) {
				Integer count = results.get(0);

				if (count.intValue() > 0) {
					return true;
				}
			}

			return false;
		}

		private MappingSqlQuery<Integer> _mappingSqlQuery;
	}

	private static final String _SQL_SELECT_BMSBOOK = "SELECT bmsBook FROM BMSBook bmsBook";
	private static final String _SQL_SELECT_BMSBOOK_WHERE = "SELECT bmsBook FROM BMSBook bmsBook WHERE ";
	private static final String _SQL_COUNT_BMSBOOK = "SELECT COUNT(bmsBook) FROM BMSBook bmsBook";
	private static final String _SQL_COUNT_BMSBOOK_WHERE = "SELECT COUNT(bmsBook) FROM BMSBook bmsBook WHERE ";
	private static final String _SQL_GETBMSBORROWINGS = "SELECT {BMS_BMSBorrowing.*} FROM BMS_BMSBorrowing INNER JOIN BMS_BMSBook ON (BMS_BMSBook.bookId = BMS_BMSBorrowing.bookId) WHERE (BMS_BMSBook.bookId = ?)";
	private static final String _SQL_GETBMSBORROWINGSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM BMS_BMSBorrowing WHERE bookId = ?";
	private static final String _SQL_CONTAINSBMSBORROWING = "SELECT COUNT(*) AS COUNT_VALUE FROM BMS_BMSBorrowing WHERE bookId = ? AND borrowingId = ?";
	private static final String _FINDER_COLUMN_BOOKTITLE_BOOKTITLE_1 = "bmsBook.bookTitle IS NULL";
	private static final String _FINDER_COLUMN_BOOKTITLE_BOOKTITLE_2 = "bmsBook.bookTitle = ?";
	private static final String _FINDER_COLUMN_BOOKTITLE_BOOKTITLE_3 = "(bmsBook.bookTitle IS NULL OR bmsBook.bookTitle = ?)";
	private static final String _FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_1 = "bmsBook.bookTitle IS NULL AND ";
	private static final String _FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_2 = "bmsBook.bookTitle = ? AND ";
	private static final String _FINDER_COLUMN_BOOKTITLE_AUTHOR_BOOKTITLE_3 = "(bmsBook.bookTitle IS NULL OR bmsBook.bookTitle = ?) AND ";
	private static final String _FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_1 = "bmsBook.author IS NULL";
	private static final String _FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_2 = "bmsBook.author = ?";
	private static final String _FINDER_COLUMN_BOOKTITLE_AUTHOR_AUTHOR_3 = "(bmsBook.author IS NULL OR bmsBook.author = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "bmsBook.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No BMSBook exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No BMSBook exists with the key {";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(BMSBookPersistenceImpl.class);
	private static BMSBook _nullBMSBook = new BMSBookImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<BMSBook> toCacheModel() {
				return _nullBMSBookCacheModel;
			}
		};

	private static CacheModel<BMSBook> _nullBMSBookCacheModel = new CacheModel<BMSBook>() {
			public BMSBook toEntityModel() {
				return _nullBMSBook;
			}
		};
}